package com.ultrapower.request;

import java.util.List;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Scan;

import com.sematext.hbase.wd.RowKeyDistributorByHashPrefix;
import com.ultrapower.bean.StringMetaDataBean;
import com.ultrapower.mapper.StringMetaDataMapper;
import com.ultrapower.util.HBaseTables;
import com.ultrapower.util.rowkey.RangeOneByteSimpleHash;

/**
 * 
 * @author wangy
 *
 */
public class StringMetaDataTable extends BaseRequest {

	private RowKeyDistributorByHashPrefix rowKeyDistributorByHashPrefix = new RowKeyDistributorByHashPrefix(
			new RangeOneByteSimpleHash(0, 32, 8));
	private int stringId = 1;

	public static void main(String[] args) {
		BaseRequest table = new StringMetaDataTable();
		table.execute();
	}

	@Override
	void execute() {
		TableName stringMetaDataTableName = this.getTableName(HBaseTables.STRING_METADATA_STR);
		StringMetaDataMapper map=new StringMetaDataMapper(rowKeyDistributorByHashPrefix);
		this.getHbaseTemplate().get(stringMetaDataTableName, createGet(), result -> {
			List<StringMetaDataBean> listStr = map.mapRow(result, 0);
			if(listStr!=null && !listStr.isEmpty()){
				listStr.forEach(c->{
					System.out.println(c);
				});
			}
			return listStr;
		});
	}

	@Override
	protected Scan createScan() {
		return null;
	}

	protected Get createGet() {

		StringMetaDataBean stringMetaData = new StringMetaDataBean(agentId, this.startTime, stringId);
		byte[] rowKey = rowKeyDistributorByHashPrefix.getDistributedKey(stringMetaData.toRowKey());
		Get get = new Get(rowKey);
		return get.addFamily(HBaseTables.STRING_METADATA_CF_STR);
	}

}
